home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWCommon / Sources / FWPriMem.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  3.1 KB  |  119 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/25/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef   FWPRIMEM_H
  13. #include "FWPriMem.h"
  14. #endif
  15.  
  16. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
  17. #include <Windows.h>
  18. #endif
  19.  
  20.  
  21. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
  22. #include <WindowsX.h>
  23. #endif
  24.  
  25. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  26. #include <Memory.h>
  27. #endif
  28.  
  29. //----------------------------------------------------------------------------------------
  30. // FW_PrimitiveAllocateBlock
  31. //----------------------------------------------------------------------------------------
  32. void* FW_PrimitiveAllocateBlock(size_t n)
  33. {
  34. #if defined(FW_BUILD_MAC)
  35.     return ::NewPtr(n);
  36. #elif defined(FW_BUILD_WIN)
  37.     return GlobalAllocPtr(GHND, n);
  38. #endif
  39. }
  40.  
  41. //----------------------------------------------------------------------------------------
  42. // FW_PrimitiveResizeBlock
  43. //----------------------------------------------------------------------------------------
  44. void* FW_PrimitiveResizeBlock(void* p,
  45.                            size_t n)
  46. {
  47. #if defined(FW_BUILD_MAC)
  48.     void* q = p;
  49.     ::SetPtrSize((Ptr) q, n);
  50.     if (MemError() != noErr)
  51.     {
  52.         q = FW_PrimitiveAllocateBlock(n);
  53.         if (q)
  54.         {
  55.             ::BlockMove(p, q, n);
  56.             FW_PrimitiveFreeBlock(p);
  57.         }
  58.     }
  59.     return q;
  60. #elif defined(FW_BUILD_WIN)
  61.     return GlobalReAllocPtr(p, n, 0);
  62. #endif
  63. }
  64.  
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // FW_PrimitiveGetBlockSize
  68. //----------------------------------------------------------------------------------------
  69. size_t FW_PrimitiveGetBlockSize(void* p)
  70. {
  71. #if defined(FW_BUILD_MAC)
  72.     return ::GetPtrSize((Ptr) p);
  73. #elif defined(FW_BUILD_WIN)
  74.     return GlobalSize(GlobalPtrHandle(p));
  75. #endif
  76. }
  77.  
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // FW_PrimitiveFreeBlock
  81. //----------------------------------------------------------------------------------------
  82. void FW_PrimitiveFreeBlock(void* p)
  83. {
  84. #if defined(FW_BUILD_MAC)
  85.     if (p)
  86.         ::DisposPtr((Ptr) p);
  87. #elif defined(FW_BUILD_WIN)
  88.     if (p)
  89.         GlobalFreePtr(p);
  90. #endif
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // FW_PrimitiveCopyMemory
  95. //----------------------------------------------------------------------------------------
  96.  
  97. void FW_PrimitiveCopyMemory(const void *source, void *destination, size_t bytes)
  98. {
  99. #if defined(FW_BUILD_MAC)
  100.     ::BlockMove(source, destination, bytes);
  101. #elif defined(FW_BUILD_WIN)
  102.     // More efficient code is desirable. Don't want to use c standard library.
  103.     // This code must work correctly for overlapping blocks.
  104.     if (source > destination)
  105.     {
  106.         char *dst = (char *) destination;
  107.         const char *src = (const char *) source;
  108.         while (bytes--)
  109.             *dst++ = *src++;
  110.     }
  111.     else if (source < destination)
  112.     {
  113.         char *dst = (char *) destination + bytes;
  114.         const char *src = (const char *) source + bytes;
  115.         while (bytes--)
  116.             *--dst = *--src;
  117.     }
  118. #endif
  119. }